package com.tz.zczc.core.service.impl;

import com.github.pagehelper.PageInfo;
import com.tz.zczc.core.service.KeywordsService;
import com.tz.zczc.dao.api.KeywordsDao;
import com.tz.zczc.domain.model.entity.Keywords;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author:Mark
 * @Description:
 * @Date: Created on 15:43 2018-12-5
 * @Modify by:
 */
@Service
public class KeywordsServiceImpl implements KeywordsService {

  @Autowired
  private KeywordsDao keywordsDao;

  @Override
  public Boolean addRecordSelective(Keywords keywords) {
    if(keywords.getKeyWord()!=null){
      keywords.setDeleted(false);
      List<Keywords> keywordsList = keywordsDao.selectBYKeywords(keywords);
      if (keywordsList==null||(keywordsList.size()<1)){
        //添加记录
        keywords.setId(UUID.randomUUID().toString());
        keywords.setFrequency(1);
        keywords.setDeleted(false);
        keywords.setSearchTime(new Date());
        return keywordsDao.addRecordSelective(keywords)>0;
      }else {
        Keywords result = keywordsList.get(0);
        result.setFrequency(result.getFrequency()+1);
        result.setSearchTime(new Date());
        //修改频率
        return keywordsDao.updateRecordSelective(result)>0;
      }
    }
    return false;
  }

  @Override
  public List<Keywords> selectBYKeywords(Keywords keywords) {
    return keywordsDao.selectBYKeywords(keywords);
  }

  @Override
  public PageInfo<Keywords> selectKeywordsByPages(Integer pageSize, Integer pageNum) {
    return keywordsDao.selectKeywordsByPages(pageSize,pageNum);
  }

  @Override
  public Boolean deleteById(List<String> ids) {
    if(ids!=null){
      return keywordsDao.deleteById(ids)>0;
    }
    return false;
  }

  @Override
  public Boolean logicDeleteById(List<String> ids) {
    if(ids!=null){
      Keywords keywords=new Keywords();
      keywords.setDeleted(true);
      return keywordsDao.logicDeleteById(ids,keywords)>0;
    }
    return false;
  }

  @Override
  public Boolean updateByid(Keywords keywords) {
    if(keywords!=null){
      return keywordsDao.updateRecordSelective(keywords)>0;
    }
    return false;
  }
}
